<?php
/**
*	作者：信呼开发团队(雨中磐石)
*	官网：http://www.rockoa.com
*	软件：信呼在线客服系统
*	服务端推送
*/
class reimChajian extends Chajian
{
	
	//初始化配置读取
	protected function initChajian()
	{
		$purl 	 = getconfig('server_push');
		if(isempt($purl))$purl = 'http://127.0.0.1:6553/';
		$reimhot = $this->getpushhostport($purl);
		$this->rockqueue_host = $reimhot['host'];
		$this->rockqueue_port = $reimhot['port'];
	}
	public function getpushhostport($str)
	{
		$host = ''; $port = 0;
		$stra = explode('//', $str);
		if(isset($stra[1])){
			$strb 	= explode(':', str_replace('/','', $stra[1]));
			$host	= $strb[0];
			$port	= (int)arrvalue($strb, 1, '0');
		}
		return array('host'=>$host,'port'=>$port);
	}
	
	/**
	*	发送队列信息
	*	$cont 内容可以是http地址，也可以如:cli,run
	*	$param 参数
	*	使用 c('reim')->push('cli,run');
	*/
	public function push($cont, $param=array(), $runtime=0, $id=0)
	{
		$type 	= 'cmd';
		$url	= $cont;
		$param['cfromurl'] = $this->rock->jm->base64encode($this->rock->url());
		if(substr($cont,0,4)=='http'){
			$type='url';
		}else{
			if(!contain($url, ','))$url='cli,'.$url.'';
			$phppath = getconfig('phppath');
			if(!contain($this->rockqueue_host, '127.0.0.1') || isempt($phppath)){
				$urla= explode(',', $url);
				$url = URL.'api.php?m='.$urla[0].'&a='.$urla[1].'';
				$type= 'url';
			}else{
				$st1 = '';
				foreach($param as $k=>$v)$st1.=' -'.$k.'='.$v.'';
				$url = ''.$phppath.' '.ROOT_PATH.'/api.php '.$url.''.$st1.'';
			}
		}
		if($type=='url'){
			$jg  = contain($url,'?')?'&':'?';
			$st1 = '';
			foreach($param as $k=>$v)$st1.='&'.$k.'='.$v.'';
			if($st1!='')$url.=''.$jg.''.substr($st1,1).'';
		}
		
		if($id==0)$id = rand(1,99999);
		$rarr[] = array(
			'qtype'		=> $type,
			'runtime'	=> $runtime,
			'url'		=> $url,
			'id'		=> $id
		);
		$barr = $this->pushdata($rarr);
		$barr['cmdurl'] = ''.$type.':'.$url;
		$this->rock->debugs($barr,'pushanay');
		return $barr;
	}
	
	/**
	*	推送数据过去
	*/
	public function pushdata($rarr)
	{
		if(!$rarr)return returneorror('发空内容');
		if(!$this->rockqueue_host || $this->rockqueue_port==0)return returnerror('没有配置服务端');
		if(is_array($rarr))$rarr = json_encode($rarr);
		$url = 'http://'.$this->rockqueue_host.':'.$this->rockqueue_port.'/?atype=send&data='.urlencode($rarr).'';
		$reqult = c('curl')->setTimeout(5)->getcurl($url);
		if($reqult){
			return returnsuccess($reqult);
		}else{
			return returnerror('服务端配置不能用');
		}
	}
	
}